package com.jy.app.lib.view.qrcode;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;

import com.google.zxing.WriterException;
import com.jy.app.lib.view.qrcode.zxing.EncodingHandler;
import com.jy.app.lib.view.qrcode.zxing.utils.Utils;
import com.jy.app.lib.view.qrcode.zxing.view.CaptureActivity;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class QrCodeManager {
	public static final int SELECT_PIC_KITKAT = 10001;
	public static final int SCANNING_QRCODE = 10002;
	/**
	 * Get Bitmap 
	 * 
	 * input content and size return bitmap
	 * if isStorage input true so bitmap will storage
	 * next time return sd storage bitmap
	 * 
	 * @param String  
	 * @param int	  
	 * @param boolean	
	 * @throws IOException 
	 * @throws WriterException 
	 * */
	public static Bitmap getQrCode(String content,int size,boolean isStorage) throws IOException, WriterException{
		if (null !=content) {
			if (isStorage) {
				return getSd(content, size);
			}else {
				return getBitmap(content, size);
			}
		}else{
		return null;
		}
	}
	
	/**
	 * start system select pic
	 * */
	public static void startPicSelect(Activity context){
		Intent intent = new Intent();  
		/* ����Pictures����Type�趨Ϊimage */  
		 intent.setType("image/*");  
		/* ʹ��Intent.ACTION_GET_CONTENT���Action */  
		 intent.setAction(Intent.ACTION_GET_CONTENT);   
		/* ȡ����Ƭ�󷵻ر����� */  
		 context.startActivityForResult(intent, SELECT_PIC_KITKAT);
	}
	
	/**
	 * get pic qr code content 
	 * this Method in startPicSelect back on onActivityResult 
	 * */
	public static String getQrCodeContent(Context context,Intent data){
		String scanResult = Utils.distinguish(context,data.getData());
		return scanResult;
	}
	
	
	/**
	 * return resultCode is RESULT_OK
	 * content Storage Intent 
	 * return type String key is result
	 * 
	 * */
	public static void startScanningQrCode(Activity context){
		Intent intent = new Intent(context,CaptureActivity.class);
		context.startActivityForResult(intent, SCANNING_QRCODE);
	}
	
	/**
	 * ��ȡsd���д���Ķ�ά��
	 * @throws WriterException 
	 * */
	private static Bitmap getSd(String content,int size) throws IOException, WriterException{
		String root= Environment.getExternalStorageDirectory().getAbsolutePath()+"/bitmaps/";
		File fileroot=new File(root);
		if (!(fileroot.exists())) {
			boolean is=fileroot.mkdirs();
		}
		String name = content+"_"+size+".png";
		String url =root+name;
		if (new File(url).exists()) {
			return BitmapFactory.decodeFile(url);
		}else {
			return setBitmapSd(content, size, url);
		}
	}
	
	/**
	 * �����ά��
	 * @throws WriterException 
	 * */
	private static Bitmap setBitmapSd(String content,int size,String url) throws IOException, WriterException{
		Bitmap bitmap = getBitmap(content, size);
        File file = new File(url);  
        FileOutputStream out = new FileOutputStream(file);
        bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
        out.flush();
        out.close();
        return bitmap;
	}
	
	/**
	 * ������ά��
	 * @throws WriterException 
	 * */
	private static Bitmap getBitmap(String content,int size) throws WriterException{
			return EncodingHandler.createQRCode(content, 350);
	}

	
	
}
